'use strict';

const Controller = require('egg').Controller;
/**
 * 200=成功
 * 301-399=错误码 
 */
class HrbeuController extends Controller {
    /**
     * 下载word
     * @returns 成功返回token
     */
    async downword() {
        const fs = require('fs');
        const { ctx, config } = this;
        let { temp_id } = ctx.request.body;
        // let userid = await ctx.service.auth.getColonyUserId();
        //生成文件夹
        let orgpath = await ctx.service.officegen.memoir(temp_id);
        ctx.body = {
            code: 200,
            msg: "成功",
            data: {
                orgpath
            }
        };
    }

    /**
     * 保存
     */
    async saveBykey() {
        const { ctx, config } = this;
        let { temp_id, content, key } = ctx.request.body;
        await ctx.model.HrbeuWinter.update({ [key]: content, update_time: new Date() }, { where: { temp_id } });
        ctx.body = {
            code: 200,
            msg: "成功",
            data: {}
        };
    }

    /**
     * 保存
     */
    async getValueByKey() {
        const { ctx, config } = this;
        let { temp_id, key } = ctx.request.body;
        let rowData = await ctx.model.HrbeuWinter.findOne({ "where": { temp_id } });;
        ctx.body = {
            code: 200,
            msg: "成功",
            data: {
                [key]: rowData[key]
            }
        };
    }

    /**
     * 获取临时的id
     */
    async getHrbTempId() {
        const { ctx, app } = this;
        let { temp_id } = ctx.request.body;
        //清空
        if (!temp_id) {
            temp_id = await app.utils.uuid.v1();
            await ctx.model.HrbeuWinter.create({ id: temp_id, temp_id, user_id: temp_id, create_time: new Date() });
        }
        ctx.body = {
            code: 200,
            msg: "成功",
            data: {
                temp_id
            }
        };
    }


}

module.exports = HrbeuController;